<template>
  <div class="app-container">
    <!--    查询对话框-->
    <el-dialog
      :title="title"
      v-model="openSearch"
      :close-on-click-modal="false"
      draggable
      width="580px"
      append-to-body
    >
      <el-form
        :model="queryParams"
        ref="queryForm"
        :inline="true"
        v-show="showSearch"
        label-width="68px"
      >
        <el-col :span="24">
          <el-form-item label-width="125px" label="销售单号" prop="somDjbh">
            <el-input
              v-model="queryParams.somDjbh"
              placeholder="请输入销售单号"
              clearable
              style="width: 400px"
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label-width="125px" prop="somPmgg">
            <span slot="label">
              <el-tooltip
                content="综合查询包含物料代码，品名规格，客户编码查询"
                placement="top"
              >
                <i class="el-icon-question"></i>
              </el-tooltip>
              综合查询
            </span>
            <el-input
              v-model="queryParams.somPmgg"
              clearable
              size="small"
              style="width: 400px"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>

        <!--        <el-col :span="24">-->
        <!--          <el-form-item label-width="125px" label="交货时间" prop="dateRange">-->
        <!--            <el-date-picker-->
        <!--              clearable-->
        <!--              size="small"-->
        <!--              v-model="dateRange"-->
        <!--              :picker-options="pickerOptions"-->
        <!--              value-format="yyyy-MM-dd HH:mm:ss"-->
        <!--              type="datetimerange"-->
        <!--              range-separator="至"-->
        <!--              start-placeholder="开始日期"-->
        <!--              end-placeholder="结束日期"-->
        <!--              :default-time="['00:00:00', '23:59:59']"-->
        <!--            ></el-date-picker>-->
        <!--          </el-form-item>-->
        <!--        </el-col>-->
        <el-col :span="24">
          <el-form-item prop="pqzt" style="padding-left: 15%">
            <el-radio-group v-model="queryParams.pqzt">
              <el-radio label="1">未排满未审核</el-radio>
              <el-radio label="2">已排满未审核</el-radio>
              <el-radio label="-1">全部</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item
            style="margin-top: 25px; display: flex; justify-content: flex-end"
          >
            <el-button @click="cancel">取消</el-button>
            <el-button type="primary" @click="handleQuery">确认</el-button>
          </el-form-item>
        </el-col>
      </el-form>
    </el-dialog>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-search"
          size="mini"
          @click="handleSearch"
          >搜索</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['marketing:arrangeMaintain:pq']"
        >
          排期
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport(1)"
          v-hasPermi="['marketing:arrangeMaintain:export']"
        >
          Excel(上)
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport(2)"
          v-hasPermi="['marketing:arrangeCheck:export']"
        >
          Excel(下)
        </el-button>
      </el-col>
      <el-col :span="2.2" style="position: absolute; right: 35px; top: 0">
        <Page
          style="margin-right: 0px"
          :current="queryParams.pageNum"
          :page-size="queryParams.pageSize"
          :total="total"
          @on-change="pageChange"
          simple
        ></Page>
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      highlight-current-row
      v-loading="loading"
      :data="somList"
      border
      stripe
      :height="tableH"
      @selection-change="handleSelectionChange"
      @row-click="getChildList"
      :header-cell-style="{ 'text-align': 'center' }"
    >
      <el-table-column type="selection" style="width: 1%" align="center" />
      <el-table-column
        label="销售单号"
        :show-overflow-tooltip="true"
        align="center"
        prop="somDjbh"
      />
      <el-table-column
        label="客户"
        :show-overflow-tooltip="true"
        align="center"
        prop="somKhdm"
      >
        <template #default="scope">
          <dict-tag :options="customerOptions" :value="scope.row.somKhdm" />
        </template>
      </el-table-column>
      <el-table-column
        label="客户编码"
        :show-overflow-tooltip="true"
        align="center"
        prop="somCustwldm"
      />
      <el-table-column
        label="本厂物料"
        :show-overflow-tooltip="true"
        align="center"
        prop="somWldm"
      />
      <el-table-column
        label="品名规格"
        :show-overflow-tooltip="true"
        style="width: 10%"
        align="center"
        prop="somPmgg"
      />
      <el-table-column
        label="订单数量"
        :show-overflow-tooltip="true"
        style="width: 1%"
        align="center"
        prop="somDdsl"
      />
      <el-table-column
        label="排期数量"
        :show-overflow-tooltip="true"
        style="width: 1%"
        align="center"
        prop="somYpsl"
      />
      <el-table-column
        label="交货日期"
        :show-overflow-tooltip="true"
        align="center"
        prop="somJhrq"
        style="width: 1%"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.somJhrq, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="备注"
        :show-overflow-tooltip="true"
        style="width: 1%"
        align="center"
        prop="somDesc"
      />
    </el-table>
    <el-table
      :row-style="iRowStyle"
      :cell-style="iCellStyle"
      :header-row-style="iHeaderRowStyle"
      :header-cell-style="iHeaderCellStyle"
      class="childTable"
      style="width: 100%"
      stripe
      border
      :height="tableHChild"
      size="mini"
      highlight-current-row
      v-loading="loading"
      :data="childJhmData"
    >
      <el-table-column
        fixed
        label="#"
        width="55px"
        align="center"
        prop="level"
      />

      <el-table-column
        label="排期单号"
        show-overflow-tooltip
        align="center"
        prop="jhmDjbh"
      />

      <el-table-column
        label="交货时间"
        :show-overflow-tooltip="true"
        align="center"
        prop="jhmPqrq"
      />

      <el-table-column label="排期数量" align="center" prop="jhmPqsl" />

      <el-table-column label="已交货数" align="center" prop="jhmJhsl" />

      <el-table-column label="备注" align="center" prop="jhmDesc" />

      <el-table-column label="排期人员" align="center" prop="createBy" />

      <el-table-column label="排期日期" align="center" prop="createTime" />

      <el-table-column label="审核人员" align="center" prop="updateBy" />

      <el-table-column label="审核日期" align="center" prop="updateTime" />

      <el-table-column
        label="操作"
        fixed="right"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            :disabled="scope.row.jhmZtbz == '2'"
            @click="handleJhmDelete(scope.row)"
            v-hasPermi="['marketing:arrangeMaintain:remove']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改营销订单维护对话框 -->
    <el-dialog
      :title="title"
      v-model="open"
      :close-on-click-modal="false"
      draggable
      width="800px"
      append-to-body
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-col :span="12">
          <el-form-item label="销售单号" prop="somDjbh">
            <el-input v-model="form.somDjbh" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="交货日期" prop="somJhrq">
            <el-date-picker
              clearable
              size="small"
              v-model="form.somJhrq"
              type="date"
              :clearable="false"
              value-format="yyyy-MM-dd"
              disabled
            ></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="物料代码" prop="somWldm">
            <el-input v-model="form.somWldm" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="物料品名" prop="somWldm">
            <el-input v-model="form.somPmgg" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="订单数量" prop="somDdsl">
            <el-input v-model="form.somDdsl" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="客户编码" prop="somCustwldm">
            <el-input v-model="form.somCustwldm" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="已排期数" prop="somYpsl">
            <el-input v-model="form.somYpsl" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="未排期数" prop="somWpsl">
            <el-input v-model="form.somWpsl" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="10">
          <el-form-item label="排期数量" prop="jhmPqsl">
            <el-input v-model="form.jhmPqsl" />
          </el-form-item>
        </el-col>
        <el-col :span="10">
          <el-form-item label="排期日期" prop="jhmPqrq" label-width="80px">
            <el-date-picker
              clearable
              size="small"
              v-model="form.jhmPqrq"
              type="date"
              :clearable="false"
              value-format="yyyy-MM-dd HH:mm:ss"
            ></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="自动排期" prop="autoFlag">
            <el-switch
              v-model="form.autoFlag"
              inactive-value="0"
              active-value="1"
              @change="handleInfFlag(form)"
            ></el-switch>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="备注" prop="jhmDesc">
            <el-input
              type="textarea"
              v-model="form.jhmDesc"
              placeholder="请输入备注"
            />
          </el-form-item>
        </el-col>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listArrange,
  insJhmUpdSom,
  getChild,
  delArrangeCheck,
  exportArrangeCheck,
  exportArrangeCheckFx,
} from "@/api/marketing/arrangeCheck";
import { getAllCustomer } from "@/api/project/customer";

export default {
  name: "ArrangeMaintain",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      currentData: [],
      //子项数组
      childJhmData: [],
      // 日期范围
      dateRange: [
        this.$moment().startOf("month").toDate(),
        this.$moment().endOf("month").toDate(),
      ],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      tableH: document.body.clientHeight / 2 - 82,
      tableHChild: document.body.clientHeight / 2 - 100,
      // 营销订单维护表格数据
      somList: [],
      // 弹出层标题
      title: "交货排期",
      // 是否显示弹出层
      open: false,
      openSearch: false,
      //客户类型
      customerOptions: [],
      //排期状态
      scheduleOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: document.body.clientHeight > 900 ? 15 : 10,
        somDjbh: null,
        somPmgg: null,
        somXdrqEnd: null,
        somXdrq: null,
        pqzt: "1",
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        jhmPqsl: [
          { required: true, message: "排期数量不能为空", trigger: "blur" },
        ],
        jhmPqrq: [
          { required: true, message: "排期日期不能为空", trigger: "blur" },
        ],
      },
      //日期初始化
      pickerOptions: {
        shortcuts: [
          {
            text: "未来一周",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() + 3600 * 1000 * 24 * 7);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "未来一个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() + 3600 * 1000 * 24 * 30);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "未来三个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() + 3600 * 1000 * 24 * 90);
              picker.$emit("pick", [start, end]);
            },
          },
        ],
      },
    };
  },
  created() {
    this.getList();
    getAllCustomer().then((response) => {
      for (let i of response.data) {
        i.dictLabel = i.cusName;
        i.dictValue = i.cusCode + "";
        i.listClass = "default";
      }
      this.customerOptions = response.data;
    });
    this.getDicts("sys_schedule_flag").then((response) => {
      for (let i of response.data) {
        if (i.dictValue === "0") {
          i.dictLabel = "未排期";
        }
      }
      this.scheduleOptions = response.data;
    });
  },
  methods: {
    iRowStyle: function ({ row, rowIndex }) {
      return "height:10px;";
    },
    iHeaderRowStyle: function ({ row, rowIndex }) {
      return "height:10px;";
    },
    iCellStyle: function ({ row, column, rowIndex, columnIndex }) {
      //新增状态
      if (row.bomTjflag === "0" && row.bomZtbz === 1) {
        return "padding:0px 0;color:rgb(0, 126, 255)";
      }
      //审核状态
      else if (row.bomZtbz === 2) {
        return "padding:0px 0;color:#bb0331";
      } else {
        return "padding:0px 0";
      }
    },
    iHeaderCellStyle: function ({ row, column, rowIndex, columnIndex }) {
      return "padding:0px;text-align:center";
    },
    /** 查询营销订单维护列表 */
    getList(row) {
      // 取消日期
      // if (this.dateRange !== null) {
      //   this.queryParams.somXdrq = this.moment(this.dateRange[0]).format('YYYY-MM-DD HH:mm:ss')
      //   this.queryParams.somXdrqEnd = this.moment(this.dateRange[1]).format('YYYY-MM-DD HH:mm:ss')
      // }
      this.loading = true;
      listArrange(this.queryParams).then((response) => {
        this.somList = response.rows;
        this.total = response.total;
        this.loading = false;
        this.childBomList = [];
        this.childJhmData = [];
        if (row != null) {
          this.getChildList(row);
        } else if (this.somList != null && this.somList.length > 0) {
          this.getChildList(this.somList[0]);
        }
      });
    },
    //查询排期详细
    getChildList(row) {
      if (row.somDjbh != null) {
        getChild(row.somDjbh).then((response) => {
          this.childJhmData = response.data;
        });
      }
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
      this.openSearch = false;
    },
    //自动排期 +30
    handleInfFlag(row) {
      if (row.autoFlag === "1") {
        const date1 = new Date(
          this.moment(this.form.somXdrq)
            .format("YYYY-MM-DD HH:mm:ss")
            .replace(/-/, "/")
        );
        const date2 = new Date(date1);
        date2.setDate(date1.getDate() + 30);
        this.form.jhmPqrq = date2;
      } else {
        this.form.jhmPqrq = null;
      }
    },
    /** 删除按钮操作 */
    handleJhmDelete(row) {
      const jhmDjbh = row.jhmDjbh;
      this.$confirm(
        '是否确认删除交货排期审核编号为"' + jhmDjbh + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(function () {
          return delArrangeCheck(row.jhmId);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    // 表单重置
    reset() {
      this.form = {
        jhmPqrq: new Date(),
        jhmPqsl: null,
        somCustwldm: null,
        somDdsl: null,
        somDesc: null,
        somDjbh: null,
        somJhrq: null,
        somKhdm: null,
        somPmgg: null,
        somWldm: null,
        somWpsl: null,
        somYpsl: null,
        somZtbz: null,
        autoFlag: false,
        somXdrq: null,
        jhmDesc: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
      this.openSearch = false;
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      if (selection.length > 1) {
        selection = selection[selection.length - 1];
      }
      this.currentData = selection;
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    handleSearch() {
      this.reset();
      this.title = "查询交货排期维护";
      this.openSearch = true;
    },
    /** 排期按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.form = this.currentData[0];
      this.form.jhmPqsl = this.form.somWpsl;
      this.open = true;
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.jhmPqsl != null && this.form.jhmPqrq != null) {
            if (this.form.jhmPqsl > this.form.somWpsl) {
              this.msgError("排期数量不能超过订单数量");
              return;
            }
            this.form.autoFlag = parseInt(this.form.autoFlag);
            this.form.jhmPqrq = this.moment(this.form.jhmPqrq).format(
              "yyyy-MM-DD HH:mm:ss"
            );
            insJhmUpdSom(this.form).then((response) => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    pageChange(page) {
      this.queryParams.pageNum = page;
      this.getList();
    },
    /** 导出按钮操作 */
    handleExport(num) {
      const queryParams = this.queryParams;
      if (num === 1) {
        this.$confirm("是否确认导出 交货排期维护-父项 数据?", "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
          .then(() => {
            this.exportLoading = true;
            return exportArrangeCheckFx(queryParams);
          })
          .then((response) => {
            this.download(response.msg);
            this.exportLoading = false;
          })
          .catch(() => {});
      } else {
        this.$confirm("是否确认导出 交货排期维护-子项 数据?", "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
          .then(() => {
            this.exportLoading = true;
            return exportArrangeCheck(queryParams);
          })
          .then((response) => {
            this.download(response.msg);
            this.exportLoading = false;
          })
          .catch(() => {});
      }
    },
  },
};
</script>

<style lang="scss" scoped></style>
